// HomeDaemon.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "HomeDaemon.h"
#include "../ParseServer/ParseServerImpl.h"
#include <string>

#define MAX_LOADSTRING 100

HANDLE g_hInstanceMutex = NULL;

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	g_hInstanceMutex = CreateMutex(NULL, FALSE, L"Global\\HomeDaemon");
	if (!g_hInstanceMutex)
		return 0;
	DWORD res = WaitForSingleObject(g_hInstanceMutex, 0);
	if (res != WAIT_OBJECT_0)
	{
		CloseHandle(g_hInstanceMutex);
		return 0;
	}
	InitServer();
	IHomeServer * pHomeServer = CreateHomeServer();
	pHomeServer->StartServer();
	eHSResult hs = pHomeServer->WaitForStop();
	if (hs != HS_SUCCESS)
	{
		const wchar_t * errstr = pHomeServer->GetErrorCode(hs);
		std::wstring info = L"Server running error : ";
		info += errstr;
		MessageBox(NULL, info.c_str(), L"Error", MB_OK);
	}
	ReleaseMutex(g_hInstanceMutex);
	CloseHandle(g_hInstanceMutex);
	return 0;
}